using BackOffice.Application.Services;
using Microsoft.AspNetCore.Mvc;
using BackOffice.Application.DTOs;

namespace BackOffice.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LogController : ControllerBase
    {
        private readonly ILogService _logService;

        public LogController(ILogService logService)
        {
            _logService = logService;
        }

        // 获取日志列表
        [HttpGet("list")]
        public async Task<IActionResult> GetLogs(
            [FromQuery] DateTime? startDate, 
            [FromQuery] DateTime? endDate, 
            [FromQuery] string? username, 
            [FromQuery] string? action,
            [FromQuery] string? level,
            [FromQuery] int page = 1,
            [FromQuery] int pageSize = 20)
        {
            var result = await _logService.GetLogsAsync(startDate, endDate, username, action, level, page, pageSize);
            return Ok(result);
        }

        // 记录操作日志
        [HttpPost("record")]
        public async Task<IActionResult> RecordLog([FromBody] LogRecordDto dto)
        {
            var result = await _logService.RecordLogAsync(dto);
            return Ok(result);
        }

        // 导出日志
        [HttpGet("export")]
        public async Task<IActionResult> ExportLogs(
            [FromQuery] DateTime? startDate, 
            [FromQuery] DateTime? endDate,
            [FromQuery] string? level)
        {
            var result = await _logService.ExportLogsAsync(startDate, endDate, level);
            return File(result, "text/csv", $"logs_{DateTime.Now:yyyyMMdd_HHmmss}.csv");
        }

        // 清理日志
        [HttpDelete("clean")]
        public async Task<IActionResult> CleanLogs([FromQuery] string beforeDate)
        {
            if (!DateTime.TryParse(beforeDate, out DateTime parsedDate))
            {
                return BadRequest(new { Message = "无效的日期格式", ReceivedDate = beforeDate });
            }
            
            var result = await _logService.CleanLogsAsync(parsedDate);
            return Ok(result);
        }

        // 获取日志统计信息
        [HttpGet("statistics")]
        public async Task<IActionResult> GetLogStatistics()
        {
            var result = await _logService.GetLogStatisticsAsync();
            return Ok(result);
        }

        // 按级别获取日志
        [HttpGet("level/{level}")]
        public async Task<IActionResult> GetLogsByLevel(
            string level,
            [FromQuery] int page = 1,
            [FromQuery] int pageSize = 20)
        {
            var result = await _logService.GetLogsByLevelAsync(level, page, pageSize);
            return Ok(result);
        }

        // 获取所有日志级别
        [HttpGet("levels")]
        public async Task<IActionResult> GetLogLevels()
        {
            var levels = new[]
            {
                new { level = "INFO", name = "信息", color = "#67C23A" },
                new { level = "WARNING", name = "警告", color = "#E6A23C" },
                new { level = "ERROR", name = "错误", color = "#F56C6C" },
                new { level = "DEBUG", name = "调试", color = "#909399" }
            };
            return Ok(levels);
        }
    }
} 